package com.hinnotek.amoeba.utilities;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Settings;

import com.xuexiang.xutil.common.StringUtils;

public final class IntentUtils {
    

    /**
     * @description 启动Activity
     * @date 2015年8月26日
     */
    public static void startActivity(Context context, Class<? extends Activity> cls) {
        startActivity(context, cls, null);
    }
    
    /**
     * @param activity
     * @param cls
     *            目标activity
     * @param bundle
     *            参数
     * @param requestCode
     *            请求码
     * @throws @Description:
     *             带返回值启动Activity
     */
    public static void startActivityForResult(Activity activity,
                                              Class<? extends Activity> cls,
                                              Bundle bundle,
                                              int requestCode) {
        Intent intent = new Intent(activity, cls);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        activity.startActivityForResult(intent, requestCode);
    }
    
    /**
     * @param bundle
     *            返回参数
     * @description startAtivityForResult返回结果
     * @date 2015年8月26日
     */
    public static void setResult(Activity activity, Bundle bundle) {
        Intent localIntent = new Intent();
        if (null != bundle) {
            localIntent.putExtras(bundle);
        }
        activity.setResult(Activity.RESULT_OK, localIntent);
        activity.finish();
    }
    
    /**
     * @param context
     *            上下文
     * @param phoneNum
     *            电话号码
     * @description 拨打电话
     * @date 2015年9月6日
     */
    public static void call(Context context, String phoneNum) {
        if (StringUtils.isEmpty(phoneNum)) {
            return;
        }
        Uri uri = Uri.parse("tel:" + phoneNum);
        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
        context.startActivity(intent);
    }

    /**
     * 跳转至本地通讯录
     * @param context
     */
    public static void addressBook(Context context) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(ContactsContract.Contacts.CONTENT_URI);
        context.startActivity(intent);
    }
    
    /**
     * 短信
     * 
     * @param context
     * @param phoneNum
     */
    public static void sendSms(Context context, String phoneNum) {
        Uri uri = Uri.parse("smsto:" + phoneNum);
        Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(sendIntent);
    }
    
    public static void startService(Context context, Class<? extends Service> cls) {
        startService(context, cls, null);
    }
    
    /**
     * @param context
     * @param cls
     * @param bundle
     *            参数
     * @description 启动Service
     * @date 2015年8月26日
     */
    public static void startService(Context context, Class<? extends Service> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        context.startService(intent);
    }
    
    /**
     * @throws @Description:
     */
    public static void startActivity(Context context, Class<? extends Activity> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    public static void sendEmail(Context context, String address, String message) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_EMAIL, address);
        intent.setType("text/csv");
        context.startActivity(intent);
    }

    /**
     * @param context
     *            上下文
     * @description 跳转到WIFI网络设置
     * @date 2020-4-15
     */
    public static void startSettingWIFINetwork(Context context) {
        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
        context.startActivity(intent);
    }
    /**
     * @param context
     *            上下文
     * @description 跳转到网络设置
     * @date 2015年9月6日
     */
    public static void settingNetwork(Context context) {
        Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
        context.startActivity(intent);
    }

    /**
     * @throws @Description:
     */
    public static void startActivityForClearTask(Context context, Class<? extends Activity> cls) {
        Intent intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }
    
    public static void startActivityForClearTop(Context context, Class<? extends Activity> cls) {
        Intent intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }
    
    public static void startActivityForClearTop(Context context, Class<? extends Activity> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

}
